package org.jeecg.modules.demo.Jobmt.service.impl;

import org.jeecg.modules.demo.Jobmt.entity.Jobmt;
import org.jeecg.modules.demo.Jobmt.entity.Wangong;
import org.jeecg.modules.demo.Jobmt.mapper.WangongMapper;
import org.jeecg.modules.demo.Jobmt.mapper.JobmtMapper;
import org.jeecg.modules.demo.Jobmt.service.IJobmtService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 报工主表
 * @Author: jeecg-boot
 * @Date:   2021-05-04
 * @Version: V1.0
 */
@Service
public class JobmtServiceImpl extends ServiceImpl<JobmtMapper, Jobmt> implements IJobmtService {

	@Autowired
	private JobmtMapper jobmtMapper;
	@Autowired
	private WangongMapper wangongMapper;
	
	@Override
	@Transactional
	public void saveMain(Jobmt jobmt, List<Wangong> wangongList) {
		jobmtMapper.insert(jobmt);
		if(wangongList!=null && wangongList.size()>0) {
			for(Wangong entity:wangongList) {
				//外键设置
				entity.setJobid(jobmt.getId());
				wangongMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Jobmt jobmt,List<Wangong> wangongList) {
		jobmtMapper.updateById(jobmt);
		
		//1.先删除子表数据
		wangongMapper.deleteByMainId(jobmt.getId());
		
		//2.子表数据重新插入
		if(wangongList!=null && wangongList.size()>0) {
			for(Wangong entity:wangongList) {
				//外键设置
				entity.setJobid(jobmt.getId());
				wangongMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		wangongMapper.deleteByMainId(id);
		jobmtMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			wangongMapper.deleteByMainId(id.toString());
			jobmtMapper.deleteById(id);
		}
	}
	
}
